package HoneWork;
interface Pet {
    public String getName();
    public String getColor();
    public int getAge();
}

class Cat implements Pet {
    private String name;
    private String color;
    private int age;
    public Cat (String name,String color,int age){
        this.age = age;
        this.color = color;
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getColor() {
        return color;
    }

    @Override
    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Dog implements Pet {
    private String name;
    private String color;
    private int age;
    public Dog (String name,String color,int age){
        this.age = age;
        this.color = color;
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getColor() {
        return color;
    }

    @Override
    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
class PetShop {
    //…………增加宠物…………
    private Pet[] pets;
    private int foot;
    public PetShop(int len) {
        if(len>0) {
            this.pets=new Pet[len];
        }
        else {
            this.pets=new Pet[1];
        }
    }
    public boolean add(Pet pet) {
        if(foot<this.pets.length) {
            this.pets[foot]=pet;
            foot++;
            return true;
        }
        else {
            return false;
        }
    }
    //…………查找宠物…………
    public Pet[] search(String keyWord) {
        Pet p[] = null;
        int count = 0;
        for(int i = 0;i < this.pets.length;i++) {
            if(this.pets[i] != null) {
                if(this.pets[i].getName().indexOf(keyWord)!=-1||
                    this.pets[i].getColor().indexOf(keyWord)!=-1);
                count++;
            }
        }
        p = new Pet[count];
        int f = 0;
        for(int i = 0;i <= this.pets.length;i++) {
            if(this.pets[i]!=null) {
                if(this.pets[i].getName().indexOf(keyWord)!=-1
                    ||this.pets[i].getColor().indexOf(keyWord)!=-1) {
                    p[f] = this.pets[i];
                    f++;
                }
            }
        }
        return p;
    }


}

public class Test_1 {
    public static void main(String[] args) {
        PetShop ps = new PetShop(5);
        ps.add(new Cat("白白","白色",2));
        ps.add(new Cat("阿黄","黄色",1));
        ps.add(new Cat("小橙","橘色",3));
        ps.add(new Dog("阿绿","绿色",3));
        ps.add(new Dog("灰灰","灰色",2));
        ps.add(new Dog("mermer","黑色",1));

        print(ps.search("白"));
    }
    public static void print(Pet P[]) {
        for(int i = 0;i < P.length;i++) {
            if(P[i]!=null) {
                System.out.println(P[i].getName()+","+P[i].getColor()+","+P[i].getAge());
            }
        }
    }
}
